package com.gui;

import com.manage.*;
import com.virtual.Run;
import javax.swing.*;
import java.awt.*;

public class RandomSelectionPanel extends JPanel {
    private Run systemManager;
    private JComboBox<ClassRoom> classSelector;
    private JLabel resultLabel;
    
    public RandomSelectionPanel(Run systemManager) {
        this.systemManager = systemManager;
        initializeUI();
        // 初始化后立即更新列表
        updateClassSelector();
        // 如果有班级，选择第一个班级
        if (classSelector.getItemCount() > 0) {
            classSelector.setSelectedIndex(0);
        }
    }
    
    private void initializeUI() {
        setLayout(new BorderLayout(10, 10));
        
        // 创建班级选择器
        JPanel topPanel = new JPanel();
        classSelector = new JComboBox<>();
        topPanel.add(new JLabel("选择班级："));
        topPanel.add(classSelector);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton randomStudentBtn = new JButton("随机抽取学生");
        JButton randomGroupBtn = new JButton("随机抽取小组");
        JButton randomGroupStudentBtn = new JButton("随机抽取小组成员");
        
        buttonPanel.add(randomStudentBtn);
        buttonPanel.add(randomGroupBtn);
        buttonPanel.add(randomGroupStudentBtn);
        
        // 创建结果显示区
        resultLabel = new JLabel("等待抽取...", SwingConstants.CENTER);
        resultLabel.setFont(new Font("宋体", Font.BOLD, 24));
        
        // 添加事件监听
        randomStudentBtn.addActionListener(e -> randomStudent());
        randomGroupBtn.addActionListener(e -> randomGroup());
        randomGroupStudentBtn.addActionListener(e -> randomGroupStudent());
        
        // 布局
        add(topPanel, BorderLayout.NORTH);
        add(resultLabel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private void updateClassSelector() {
        classSelector.removeAllItems();
        for (ClassRoom classRoom : systemManager.getClasses()) {
            classSelector.addItem(classRoom);
        }
    }
    
    private void randomStudent() {
        ClassRoom selectedClass = (ClassRoom) classSelector.getSelectedItem();
        if (selectedClass != null) {
            Student student = selectedClass.getRandomStudent();
            if (student != null) {
                resultLabel.setText("抽中学生：" + student.getName());
            } else {
                resultLabel.setText("该班级没有学生！");
            }
        } else {
            JOptionPane.showMessageDialog(this, 
                "请先选择一个班级！", 
                "提示", 
                JOptionPane.WARNING_MESSAGE);
        }
    }
    
    private void randomGroup() {
        ClassRoom selectedClass = (ClassRoom) classSelector.getSelectedItem();
        if (selectedClass != null) {
            Group group = selectedClass.getRandomGroup();
            if (group != null) {
                resultLabel.setText("抽中小组：" + group.getName());
            } else {
                resultLabel.setText("该班级没有小组！");
            }
        } else {
            JOptionPane.showMessageDialog(this, 
                "请先选择一个班级！", 
                "提示", 
                JOptionPane.WARNING_MESSAGE);
        }
    }
    
    private void randomGroupStudent() {
        ClassRoom selectedClass = (ClassRoom) classSelector.getSelectedItem();
        if (selectedClass != null) {
            Group group = selectedClass.getRandomGroup();
            if (group != null) {
                Student student = group.getRandomMember();
                if (student != null) {
                    resultLabel.setText("抽中" + group.getName() + 
                        "小组的" + student.getName());
                } else {
                    resultLabel.setText("该小组没有成员！");
                }
            } else {
                resultLabel.setText("该班级没有小组！");
            }
        } else {
            JOptionPane.showMessageDialog(this, 
                "请先选择一个班级！", 
                "提示", 
                JOptionPane.WARNING_MESSAGE);
        }
    }
    
    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (visible) {
            // 每次面板变为可见时更新数据
            updateClassSelector();
            if (classSelector.getItemCount() > 0) {
                classSelector.setSelectedIndex(0);
            }
            resultLabel.setText("等待抽取...");
        }
    }
} 